package com.yifeng.repo.controller.excel.loader.task.handler;

import com.alibaba.excel.support.ExcelTypeEnum;
import com.google.common.base.Strings;
import com.yifeng.repo.base.utils.file.FileHelper;
import com.yifeng.repo.controller.excel.loader.dto.DataTaskTypeEnum;
import org.apache.poi.util.TempFile;

import java.io.File;

/**
 * Created by daibing on 2023/4/17.
 */
public class ExcelFileHandler {
    private static final String HTTP_PREFIX = "http";

    public static boolean isHttpFile(String url) {
        return url.toLowerCase().startsWith(HTTP_PREFIX);
    }

    /**
     * 获取数据转存文件
     */
    public static String getDataDumpFilePath(String requestKey, DataTaskTypeEnum type) {
        String dumpFilePath = System.getProperty("excel.dump.file.path");
        String prefix = !Strings.isNullOrEmpty(dumpFilePath) ? dumpFilePath : System.getProperty(TempFile.JAVA_IO_TMPDIR);
        prefix = prefix.endsWith(File.separator) ? prefix : prefix + File.separator;
        return prefix + "excel" + File.separator + type.name().toLowerCase() + File.separator + requestKey + ExcelTypeEnum.XLSX.getValue();
    }

    public static String getDataDumpZipFilePath(String zipFileName, DataTaskTypeEnum type) {
        String dumpFilePath = System.getProperty("excel.dump.file.path");
        String prefix = !Strings.isNullOrEmpty(dumpFilePath) ? dumpFilePath : System.getProperty(TempFile.JAVA_IO_TMPDIR);
        prefix = prefix.endsWith(File.separator) ? prefix : prefix + File.separator;
        return prefix + "excel" + File.separator + type.name().toLowerCase() + File.separator + zipFileName + ".zip";
    }

    /**
     * 获取导入excel对应的ok日志结果文件
     */
    public static File getOkLogFile(String dataDumpFilePath) {
        return new File(FileHelper.newFilePath(dataDumpFilePath, "-ok"));
    }

    /**
     * 获取导入excel对应的error日志结果文件
     */
    public static File getErrorLogFile(String dataDumpFilePath) {
        return new File(FileHelper.newFilePath(dataDumpFilePath, "-error"));
    }

    /**
     * 获取导出excel对应的模板文件
     */
    public static File getTemplateFile(String dataDumpFilePath) {
        return new File(FileHelper.newFilePath(dataDumpFilePath, "-template"));
    }

    /**
     * 获取导入excel对应的加密文件
     */
    public static File getCryptFile(String dataDumpFilePath) {
        return new File(FileHelper.newFilePath(dataDumpFilePath, "-crypt"));
    }
}
